package com.airport.schedule.model;

import com.airport.schedule.ConflictHelper;
import com.airport.schedule.util.DateUtil;
import com.airport.schedule.util.StringUtils;
import edu.stanford.nlp.util.HasInterval;
import edu.stanford.nlp.util.Interval;

import java.text.ParseException;


public class Flight implements HasInterval<Integer> {

    private static final String INTERNATIONAL = "国际";
    private static final String INTERNAL = "国内";
    private static final String MIX = "混合";
    private static final String TASK_I = "I"; //任务为I的航班不载客

    private final String airCompany; // 航空公司
    private final String flightId; // 航班号
    private final String flightTask; // 飞行任务
    private final String airplaneType; // 机型
    private final int arrivalTime; // 进港时间
    private final int departureTime; // 出港时间
    private final int numPeople; // 航班人数
    private final byte flightType; // 航班类型 1国内 2国际 混合航班相当于国际航班
    private final boolean isStayOverNight; // 是否为过夜航班

    public Flight(String airCompany, String flightId, String arrivalTime,
                  String departureTime, String flightType, String flightTask,
                  String airplaneType, String numPeople) {
        this.airCompany = airCompany;
        this.airplaneType = airplaneType;
        this.flightId = flightId;
        this.flightTask = flightTask;
        this.isStayOverNight = DateUtil.isStayOverNightFlight(arrivalTime, departureTime);
        this.arrivalTime = (int)(Long.valueOf(arrivalTime)/1000);
        this.departureTime = (int)(Long.valueOf(departureTime)/1000);
        if(flightType.equals(INTERNATIONAL) || flightType.equals(MIX)) {
            this.flightType = 2;
        }else if(flightType.equals(INTERNAL)) {
            this.flightType = 1;
        }else {
            throw new IllegalArgumentException("flight "+ flightId+" has unknown flight type "+flightType);
        }
        if(StringUtils.isEmpty(numPeople)) {
            this.numPeople = 0;
        }else{
            if(!flightTask.equals(TASK_I)) {
                this.numPeople = Integer.valueOf(numPeople);
            }else{
                this.numPeople = 0;
            }
        }
    }

    // 测试用的
    public Flight(String fId, int arrivalTime, int departureTime) {
        this.arrivalTime = arrivalTime;
        this.departureTime = departureTime;
        this.airCompany = null;
        this.airplaneType = null;
        this.flightId = fId;
        this.flightTask = null;
        this.flightType = 1;
        this.numPeople = 0;
        this.isStayOverNight = false;
    }

    // 是否正常航班
    public boolean isNormalFlight() {
        return arrivalTime < departureTime;
    }

    public String getFlightId() {
        return flightId;
    }

    public int getArrivalTime() {
        return arrivalTime;
    }

    public String getAirplaneType() {
        return airplaneType;
    }

    public String getFlightTask() {
        return flightTask;
    }

    public byte getFlightType() {
        return flightType;
    }

    public int getDepartureTime() {
        return departureTime;
    }

    public String getAirCompany() {
        return airCompany;
    }

    public int getNumPeople() {
        return numPeople;
    }

    public boolean isStayOverNight() {
        return isStayOverNight;
    }

    public boolean isCargoPlane() {
        return flightTask.equals(TASK_I);
    }

    public boolean isBusinessFlight() {
        return airCompany.equals("YT");
    }

    @Override
    public Interval<Integer> getInterval() {
        return Interval.toInterval(this.arrivalTime - ConflictHelper.HALF_FLIGHT_CONFLICT_MARGIN,
                this.departureTime + ConflictHelper.HALF_FLIGHT_CONFLICT_MARGIN,
                Interval.INTERVAL_OPEN_BEGIN | Interval.INTERVAL_OPEN_END);
    }

    public String toString() {
        return "公司: "+airCompany+" 航班号:"+ flightId +" 进港时间:"+ DateUtil.format(arrivalTime)+" 出港时间:"
                +DateUtil.format(departureTime)+" "+flightType+" 任务:"+flightTask+" 机型:"+airplaneType+" 人数:"+numPeople;
    }

}
